<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class goods_model extends base_model {

	public function __construct() {
		parent::__construct();
	}
	
	//项目主类型
	public function projects() {
		$sql = "SELECT * FROM {$this->tbp}goods_project ORDER BY id ASC";
		$row = $this->db->get_results($sql);
		return $row;
	}

	//产品列表
	public function get_list($id) {
		$sql = "SELECT * FROM {$this->tbp}goods WHERE status=1 ORDER BY sort ASC, id DESC LIMIT 10";
		$rs = $this->db->get_results($sql);
		return $rs;
	}

	//获取产品详情
	public function detail($goods_id, $show_origin_pic=false, $status='1') {
		$where = '';
		if (strlen($status)) $where = " AND g.status='{$status}'";
		$sql = "SELECT g.*,
			NULL as pics, NULL as specs, NULL as country, '' as sale_method_name, 0 as favorited
			FROM {$this->tbp}goods g
			WHERE g.id='{$goods_id}' {$where}";
		$row = $this->db->get_row($sql);
		if ($row) {
			$row->price = $this->get_min_price(array($row->price, $row->promote_price));
			$row->pics = $this->get_pics($goods_id, $show_origin_pic);
			$row->specs = $this->get_specs($goods_id, true);
			//$row->country = $this->get_country($row->country_id);
			//$row->sale_method_name = $this->sale_method_name($row->sale_method);
			$row->favorited = $this->favorited($goods_id, $this->member_id);
			$row = add_domain_deep($row, array("pic", "pic"));
		}
		return $row;
	}

	//强制获取产品详情，不用判断是否下架
	public function get_detail($goods_id, $show_origin_pic=false) {
		return $this->detail($goods_id, $show_origin_pic, '');
	}

	//该商品是否已经收藏
	public function favorited($goods_id, $member_id) {
		$favorited = 0;
		if ($member_id) {
			$sql = "SELECT count(*) FROM {$this->tbp}favorite WHERE item_id='{$goods_id}' AND member_id='{$member_id}'";
			$favorited = intval($this->db->get_var($sql));
		}
		return $favorited;
	}

	//获取商品的产地
	public function get_country($country_id) {
		if ($country_id) {
			$sql = "SELECT id, name, flag_pic FROM {$this->tbp}country WHERE id='{$country_id}'";
			$row = $this->db->get_row($sql);
			return $row;
		}
	}

	//获取商品的图片
	public function get_pics($goods_id, $show_origin_pic=false) {
		$sql = "SELECT * FROM {$this->tbp}goods_pic WHERE goods_id='{$goods_id}' ORDER BY id ASC";
		$pics = $this->db->get_results($sql);
		$pics = add_domain_deep($pics, array("pic"));
		//为商品增加缩略图
		if (!$show_origin_pic && $pics) {
			$num = count($pics);
			if ($num==1) {
				$size = "big";
			} else if ($num==2) {
				$size = "medium";
			} else {
				$size = "small";
			}
			foreach ($pics as $k=>$p) {
				if (strpos($p->pic, '!') !== FALSE) continue;
				$pics[$k]->pic = get_upyun_thumb_url($p->pic, $size);
			}
		}
		return $pics;
	}

	//规格
	//20160120 by ajsong 因为APP界面原因,需要检测是否只有一条规格记录且名称为默认规格,是的话就返回空, $allways_show不检测是否只有一条记录
	public function get_specs($goods_id, $allways_show=false) {
		if (!$allways_show) {
			$sql = "SELECT count(*) FROM {$this->tbp}goods_spec WHERE goods_id='{$goods_id}'";
			if (intval($this->db->get_var($sql))==1) {
				$sql = "SELECT spec FROM {$this->tbp}goods_spec WHERE goods_id='{$goods_id}'";
				$spec = $this->db->get_var($sql);
				if ($spec == '默认规格') return array();
			}
		}
		$sql = "SELECT * FROM {$this->tbp}goods_spec WHERE goods_id='{$goods_id}' ORDER BY id ASC";
		$specs = $this->db->get_results($sql);
		foreach ($specs as $k=>$g) {
			$specs[$k]->price = $this->get_min_price(array($g->price, $g->promote_price));
		}
		return $specs;
	}

	//获取某个规格下的价格
	public function get_spec_price($goods_id, $spec) {
		$sql = "SELECT price, promote_price FROM {$this->tbp}goods_spec WHERE goods_id='{$goods_id}' AND spec='{$spec}'";
		$row = $this->db->get_row($sql);
		$price = $this->get_min_price(array($row->price, $row->promote_price));
		return $price;
	}
	
	//获取最低价格
	public function get_min_price($prices=array()) {
		asort($prices);
		foreach ($prices as $price) {
			if ($price>0) {
				return $price;
				break;
			}
		}
		return $prices[0];
	}

	//状态
	public function status_name($status) {
		$name = $status;
		switch ($status) {
			case 0:
				$name = '下架';
				break;
			case 1:
				$name = '正常';
				break;
		}
		return $name;
	}

	//销售方式：1，保税直发，2：海外直采
	public function sale_method_name($sale_method) {
		$name = '';
		switch ($sale_method) {
			case 1:
				$name = '保税直发';
				break;
			case 2:
				$name = '海外直采';
				break;
		}
		return $name;
	}

	//获取分类
	public function get_categorys($parent_id=0) {
		$sql = "SELECT *, NULL as children FROM {$this->tbp}goods_category WHERE status='1' AND parent_id='{$parent_id}' ORDER BY sort ASC, id ASC";
		$rs = $this->db->get_results($sql);
		if ($rs) {
			foreach ($rs as $key=>$g) {
				$sql = "SELECT count(*) FROM {$this->tbp}goods_category WHERE parent_id='{$g->id}'";
				if ($this->db->get_var($sql)) $rs[$key]->children = $this->get_categorys($g->id);
			}
		}
		return $rs;
	}
	
	//生成分类的option, separated,parents_and_me不用设置,函数递归用, attributes键值:key自定义属性名称,value在categorys里的字段名
	public function set_categorys_option($categorys, $selected_id=0, $attributes=array(), $separated='', $parents_and_me='') {
		$html = '';
		foreach ($categorys as $k=>$g) {
			$html .= '<option value="'.$g->id.'" tree="'.$parents_and_me.$g->id.'"';
			if ($g->id==$selected_id) $html .= ' selected';
			foreach ($attributes as $name=>$value) {
				$html .= " {$name}=\"".preg_replace_callback('/\((\w+)\)/', function($matches)use($g){
						return str_replace('"', '\"', isset($g->$matches[1])?$g->$matches[1]:'');
					}, $value)."\"";
			}
			$html .= '>'.$separated.($k==count($categorys)-1?'└':'├').$g->name.'</option>';
			if (is_array($g->children) && count($g->children)) {
				$html .= $this->set_categorys_option($g->children, $selected_id, $attributes, '　'.$separated, $parents_and_me.$g->id.',');
			}
		}
		return $html;
	}

	//获取分类与所有上级的id
	public function get_category_parents_tree($category_id) {
		$ids = $category_id;
		$sql = "SELECT parent_id FROM {$this->tbp}goods_category WHERE id='{$category_id}'";
		$row = $this->db->get_row($sql);
		if ($row && $row->parent_id>0) $ids = $this->get_category_parents_tree($row->parent_id) . ',' . $ids;
		return $ids;
	}

	//获取分类与所有下级的id
	public function get_category_children_tree($category_id) {
		$ids = $category_id;
		$sql = "SELECT id FROM {$this->tbp}goods_category WHERE parent_id='{$category_id}'";
		$rs = $this->db->get_results($sql);
		if ($rs) {
			foreach ($rs as $k=>$g) {
				$ids .= ','.$g->id;
				$sql = "SELECT count(*) FROM {$this->tbp}goods_category WHERE parent_id='{$g->id}'";
				if ($this->db->get_var($sql)) $ids .= ','.$this->get_category_children_tree($g->id);
			}
		}
		$ids = trim($ids, ',');
		return $ids;
	}
}
